import config from './config.js'

const baseUrl = "http://" + config.url



//带Token请求
const TokenRequest = (opts, data) => {
	//此token是登录成功后后台返回保存在storage中的
	let token = "";
	if (uni.getStorageSync('token')) {
		token = uni.getStorageSync('token');
	}
	// console.log(token)
	//设置默认请求方式
	let method = 'post'
	if (opts.method) {
		method = opts.method
	}
	let promise;
	//配置一下请求参数
	let DefaultOpts = {
		url: baseUrl + opts.url,
		data: data,
		method: method,
		header: method.toLowerCase() == 'get' ? {
			'token': token,
			'X-Requested-With': 'XMLHttpRequest',
			"Accept": "application/json",
			"Content-Type": "application/json; charset=UTF-8"
		} : {
			'token': token,
			'X-Requested-With': 'XMLHttpRequest',
			'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
		},
		dataType: 'json',
	}
	promise = new Promise(function(resolve, reject) {
		uni.request(
			DefaultOpts
		).then(res => {
			console.log(res);
			resolve(res[1].data);
		}).catch(err => {
			console.log("err:" + err)
			reject(err);
		})
	})
	return promise
}
export default {
	TokenRequest
}
